<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Json{
	var $page       =0;
	var $t_pages    =0;
	var $per_page   =0;
	var $ar_data    =array();
	var $_str       ='';
	var $id         ='';
	public $responce;

	function Json($data=null,$id=null){
		$this->ci =& get_instance();
		if(is_array($data))
			if(count($data)>0)
				$this->ar_data = $data;
			else
				$this->ar_data = null;
			if($id)
				$this->id = $id;
	}

	function _table(){
		$responce->page   =$this->page;
		$responce->total  =$this->t_pages;
		$responce->records=$this->per_page;

		if($this->ar_data){
			$i=0;
			foreach($this->ar_data AS $row){
				$responce->rows[$i]['id']=$row[$this->id];
				$campos=array();
				foreach($row AS  $key=>$value){
					$campos[]=$row[$key];
				}
				$responce->rows[$i]['cell']=$campos;
				$i++;
			}
		}
		return $responce;
	}

	function _head(){

	}

	function _escape($val){
		return $val;
	}

	function build(){
		$this->_head();
		$responce=$this->_table();

		return json_encode($responce);
	}

	function output(){
		$output = $this->build();
		echo $output;

	}
}
?>
